package com.example.dryclean_admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dryclean_admin.common.QueryPageParam;
import com.example.dryclean_admin.common.Result;
import com.example.dryclean_admin.entity.Orderlist;
import com.example.dryclean_admin.service.IOrderlistService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lijiaxin
 * @since 2023-12-27
 */
@RestController
@RequestMapping("/orderlist")
public class OrderlistController {
    @Autowired
    public IOrderlistService iOrderlistService;

    @RequestMapping("/list")
    public Result list(){
        return Result.success(iOrderlistService.list());
    }

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Orderlist orderlist){
        return iOrderlistService.save(orderlist)?Result.success():Result.fail();
    }

    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Orderlist orderlist){
        return iOrderlistService.updateById(orderlist)?Result.success():Result.fail();
    }

    /*public Result login(@RequestParam("account") String account, @RequestParam("password") String password){
        //1.非空校验
        if(!StringUtils.hasLength(account) || !StringUtils.hasLength((password))){
            return Result.fail();
        }
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
                .eq(User::getAccount,account)
                .eq(User::getPassword,password);
        User user = userService.getOne(wrapper);
        if(user != null){
            List menuList = iMenuService.lambdaQuery().like(Menu::getMenuRight,user.getRoleid()).list();
            HashMap res = new HashMap();
            res.put("user",user);
            res.put("menu",menuList);
            return Result.success(res);
        }
        return Result.fail();
    }*/

    @RequestMapping("/updatestate")
    public Result update(@RequestParam String id, @RequestParam String state){
        LambdaQueryWrapper<Orderlist> wrapper = new LambdaQueryWrapper<Orderlist>();
        if(StringUtils.hasLength(id) && !"null".equals(id)){

            wrapper.eq(Orderlist::getId,id);
            Orderlist orderlist = iOrderlistService.getOne(wrapper);
            orderlist.setState(state);
            iOrderlistService.updateById(orderlist);
            return Result.success();
        }
        return Result.fail();

        /*int id = Integer.parseInt(map.get("id"));
        String state = map.get("state");


        UpdateWrapper<Orderlist> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",id);
        updateWrapper.set("state",state);


        return Result.success();*/

    }

    @GetMapping("/del")
    public Result del(@RequestParam Integer id){
        return iOrderlistService.removeById(id) ? Result.success() : Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String username = (String)param.get("username");
        Page<Orderlist> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<Orderlist> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.hasLength(username) && !"null".equals(username)){
            lambdaQueryWrapper.like(Orderlist::getUsername,username);
        }
        IPage result = iOrderlistService.page(page,lambdaQueryWrapper);
        return Result.success(result.getRecords(), result.getTotal());
    }

    @RequestMapping("/userlist")
    public Result userlistPage(@RequestParam String username){
        List list = iOrderlistService.lambdaQuery().eq(Orderlist::getUsername,username).list();
        return Result.success(list);
    }
}
